package cn.rui.service.impl;

import cn.rui.mapper.UserMapper;
import cn.rui.pojo.User;
import cn.rui.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 徽州大都督
 * @date 2021/4/16
 */

@Service
public class UserServiceImpl implements UserService {

//    @Autowired
//    private PasswordEncoder passwordEncoder;

    private Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    //使用mybaitis-plus作为持久层
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //log.warn("===============登录用户为:{}=============",username);

        QueryWrapper<User> wrapper = new QueryWrapper<> ();
        wrapper.eq ("username",username);
        User user = userMapper.selectOne (wrapper);
        //log.warn("查询出的admin:{}",user.getUsername ());

//        if (user==null){
//            throw new InternalAuthenticationServiceException ("用户名不存在");
//        }

        String role = "add";

        List<GrantedAuthority> authorities =new ArrayList<> ();

        //List<String> roles = CustomUserDetailService.user.getRoles ();
        //for (String role : roles) {
        authorities.add (new SimpleGrantedAuthority ("ROLE_"+role));
        //}
        //System.out.println ("用户user："+user);

        org.springframework.security.core.userdetails.User userDetails = new org.springframework.security.core.userdetails.User (
                username, user.getPassword (),authorities
        );


        HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();

        request.getSession ().setAttribute ("loginName",username);

        //System.out.println ("信息"+userDetails);


        return userDetails;


    }

    @Override
    public Integer register(@Validated @RequestBody User user) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        user.setPassword (passwordEncoder.encode (user.getPassword ()));
        int insert = userMapper.insert (user);
        return insert;
    }
}
